package com.whalefin.service.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whalefin.domain.Advertise;
import com.whalefin.domain.LeaveMessage;
import com.whalefin.domain.User;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sf on 2015/5/15.
 */

public class UploadTool {

        public BooleanResult uploadLeaveMessageLocation(@RequestParam Double lng,@RequestParam Double lat,@RequestParam List<LeaveMessage> leaveMessageList){

        BigDecimal lngFirst=new BigDecimal(Double.toString(lng));
        Double lngLocation=lngFirst.setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();//取后六位经度

        BigDecimal latFirst=new BigDecimal(Double.toString(lat));
        Double latLocation=latFirst.setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();//取后六位纬度

        //List<User> userList = userMapper.queryAllUser();//查询所有用户
        Double lng_location = 0.0;
        Double lat_location = 0.0;
        JSONArray list2 = new JSONArray();
        JSONArray list5 = new JSONArray();
        JSONArray allList = new JSONArray();
        if(leaveMessageList != null && leaveMessageList.size() > 0){
            for(int i=0;i<leaveMessageList.size();i++){
                User user = new User();
                if(leaveMessageList.get(i).getLng() != null && leaveMessageList.get(i).getLat() != null){
                    //计算上传经纬度与其他用户的距离
                    lng_location = leaveMessageList.get(i).getLng();
                    lat_location = leaveMessageList.get(i).getLat();
                    Double a = rad(lngLocation) - rad(lng_location);
                    Double b = rad(latLocation) - rad(lat_location);
                    Double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad(lat)) * Math.cos(rad(lat_location)) * Math.pow(Math.sin(b / 2), 2)));
                    s = s * EARTH_RADIUS;
                    //组装数据,返回用户信息
                    user.setId(leaveMessageList.get(i).getUserId());
                    user.setDistance(s);
                    JSONObject userObj =(JSONObject) JSON.toJSON(user);//转为json对象
                    if(s<=2.0){//返回2公里内的用户信息
                        list2.add(userObj);
                    }
                    if(s<=5.0){//返回5公里内的用户信息
                        list5.add(userObj);
                    }
                    if(s != null){
                        allList.add(userObj);//返回所有
                    }
                }
            }
        }
        BooleanResult booleanResult = new BooleanResult(true,"用户信息");
        booleanResult.getResultMap().put("distanceLessThan2",list2);
        booleanResult.getResultMap().put("distanceLessThan5",list5);
        booleanResult.getResultMap().put("distanceAllList",allList);
        return booleanResult;
    }


    public BooleanResult uploadAdvertiseLocation(@RequestParam Double lng,@RequestParam Double lat,@RequestParam List<Advertise> advertiseList){

        BigDecimal lngFirst=new BigDecimal(Double.toString(lng));
        Double lngLocation=lngFirst.setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();//取后六位经度

        BigDecimal latFirst=new BigDecimal(Double.toString(lat));
        Double latLocation=latFirst.setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();//取后六位纬度

        //List<User> userList = userMapper.queryAllUser();//查询所有用户
        Double lng_location = 0.0;
        Double lat_location = 0.0;
        JSONArray list2 = new JSONArray();
        JSONArray list5 = new JSONArray();
        JSONArray allList = new JSONArray();
        if(advertiseList != null && advertiseList.size() > 0){
            for(int i=0;i<advertiseList.size();i++){
                User user = new User();
                if(advertiseList.get(i).getLng() != null && advertiseList.get(i).getLat() != null){
                    //计算上传经纬度与其他用户的距离
                    lng_location = advertiseList.get(i).getLng();
                    lat_location = advertiseList.get(i).getLat();
                    Double a = rad(lngLocation) - rad(lng_location);
                    Double b = rad(latLocation) - rad(lat_location);
                    Double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad(lat)) * Math.cos(rad(lat_location)) * Math.pow(Math.sin(b / 2), 2)));
                    s = s * EARTH_RADIUS;
                    //组装数据,返回用户信息
                    user.setId(advertiseList.get(i).getUserId());
                    user.setDistance(s);
                    JSONObject userObj =(JSONObject) JSON.toJSON(user);//转为json对象
                    if(s<=2.0){//返回2公里内的用户信息
                        list2.add(userObj);
                    }
                    if(s<=5.0){//返回5公里内的用户信息
                        list5.add(userObj);
                    }
                    if(s != null){
                        allList.add(userObj);//返回所有
                    }
                }
            }
        }
        BooleanResult booleanResult = new BooleanResult(true,"用户信息");
        booleanResult.getResultMap().put("distanceLessThan2",list2);
        booleanResult.getResultMap().put("distanceLessThan5",list5);
        booleanResult.getResultMap().put("distanceAllList",allList);
        return booleanResult;
    }




    //计算地图上两点距离辅助方法
    private static final double EARTH_RADIUS = 6378.137;// 赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
}
